package com.molyfun.parents.im.chatrow;

import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.view.View;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.hyphenate.chat.EMMessage;
import com.hyphenate.easeui.widget.chatrow.EaseChatRowVoicePlayClickListener;
import com.molyfun.parents.MFPApplication;
import com.molyfun.parents.R;
import com.molyfun.parents.im.ChatRowVoicePlayClickListener;
import com.molyfun.parents.im.model.ChatMessageEntity;
import com.molyfun.parents.im.model.NormalFileMessageBody;
import com.molyfun.parents.utils.StringUtils;

public class ChatRowVoice extends ChatRowFile{

    private ImageView voiceImageView;
    private TextView voiceLengthView;
    private ImageView readStatusView;
    private TextView mNickName;
    private boolean mShowNickName;

    public ChatRowVoice(Context context, ChatMessageEntity message, int position, BaseAdapter adapter) {
        super(context, message, position, adapter);
    }

    public ChatRowVoice(Context context, ChatMessageEntity message, int position, boolean showNickName, BaseAdapter adapter) {
        super(context, message, position, adapter);
        mShowNickName = showNickName;
    }

    @Override
    protected void onInflateView() {
        if(message.isUseNative()){
            inflater.inflate(R.layout.ease_row_sent_voice, this);
        }else{
            inflater.inflate(message.direct() == EMMessage.Direct.RECEIVE ?
                    R.layout.ease_row_received_voice : R.layout.ease_row_sent_voice, this);
        }
    }

    @Override
    protected void onFindViewById() {
        voiceImageView = ((ImageView) findViewById(R.id.iv_voice));
        voiceLengthView = (TextView) findViewById(R.id.tv_length);
        readStatusView = (ImageView) findViewById(R.id.iv_unread_voice);
        mNickName = (TextView) findViewById(R.id.tv_userid);
    }

    @Override
    protected void onSetUpView() {
        NormalFileMessageBody voiceBody = (NormalFileMessageBody) message.getBody();
        ChatMessageEntity.ErrorNativeBody errorNativeBody = message.getErrorNativeBody();
        int len = voiceBody.getLength();
        if(message.isUseNative()){
            if(errorNativeBody != null){
                len = errorNativeBody.getLength();
            }
        }

        if(len>0){
            if(message.isUseNative()){
                if(errorNativeBody != null){
                    voiceLengthView.setText(errorNativeBody.getLength() + "\"");
                }
            }else{
                voiceLengthView.setText(voiceBody.getLength() + "\"");
            }
            voiceLengthView.setVisibility(View.VISIBLE);
        }else{
            voiceLengthView.setVisibility(View.INVISIBLE);
        }
        if (EaseChatRowVoicePlayClickListener.playMsgId != null
                && EaseChatRowVoicePlayClickListener.playMsgId.equals(message.getId()) && EaseChatRowVoicePlayClickListener.isPlaying) {
            AnimationDrawable voiceAnimation;
            if(message.isUseNative()){
                voiceImageView.setImageResource(R.drawable.voice_to_icon);
            }else{
                if (message.direct() == EMMessage.Direct.RECEIVE) {
                    voiceImageView.setImageResource(R.drawable.voice_from_icon);
                } else {
                    voiceImageView.setImageResource(R.drawable.voice_to_icon);
                }
            }
            voiceAnimation = (AnimationDrawable) voiceImageView.getDrawable();
            voiceAnimation.start();
        } else {
            if(message.isUseNative()){
                voiceImageView.setImageResource(R.drawable.ease_chatto_voice_playing_f3);
            }else{
                if (message.direct() == EMMessage.Direct.RECEIVE) {
                    voiceImageView.setImageResource(R.mipmap.ic_chatfrom_voice_playing);
                } else {
                    voiceImageView.setImageResource(R.drawable.ease_chatto_voice_playing_f3);
                }
            }
        }

        if(mShowNickName) {
            if(message.direct() == EMMessage.Direct.RECEIVE){
                mNickName.setText(StringUtils.getSpltString(message.getUsername(), 10));
            }else{
                mNickName.setText("[" + StringUtils.getSpltString(MFPApplication.getInstance().getUser().getStudentname(), 10) + "]" + StringUtils.getSpltString(MFPApplication.getInstance().getUser().getUsername(), 10));
            }
        }


        // until here, handle sending voice message
//        handleSendMessage();
    }

    @Override
    protected void onUpdateView() {
        super.onUpdateView();
    }

    @Override
    protected void onBubbleClick() {
        System.out.println("ChatRowVoice--onBubbleClick->" + message.toString());
        new ChatRowVoicePlayClickListener(message, voiceImageView, adapter, activity).onClick(bubbleLayout);
    }
    
    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        if (ChatRowVoicePlayClickListener.currentPlayListener != null && EaseChatRowVoicePlayClickListener.isPlaying) {
            ChatRowVoicePlayClickListener.currentPlayListener.stopPlayVoice();
        }
    }
    
}
